class ElementWindow 
{
  int _x, _y, _absX, _absY;
  int _width, _height;
  
  ElementWindow parent;
  List<ElementWindow> children;
  Element element;
   
  ElementWindow(){
    _x = 0;
    _y = 0;
    _absX = 0;
    _absY = 0;
	
    parent = null;
    element = null;
    children = new List<ElementWindow>();    
  }
  
  int get width()
  {
    return element.width;
  }
  
  int get height()
  {
    return element.height;
  }
  
  void onPositionChange()
  {
    updateAbsPos();
  }
  
  void setPosition(int newX, int newY)
  {
    this._x = newX;
    this._y = newY;
	
    onPositionChange();
  }
  
  void incX(int disp)
  {
    this._x += disp;
    onPositionChange();
  }
  
  void incY(int disp)
  {
    this._y+= disp;
    onPositionChange();
  }
  
  void setX(int newX)
  {
    this._x = newX;
    onPositionChange();
  }
  
  void setY(int newY)
  {
    this._y= newY;
    onPositionChange();
  }
  
  int getX()
  {
    return _x;
  }
  
  int getY()
  {
    return _y;
  }
  
  Element getElement() {
    return this.element;
  }
  
  void updateAbsPos()
  {
    if(parent != null)
    {
      this._absX = this._x + parent.getAbsX();
      this._absY = this._y + parent.getAbsY();
    }
    else
    {
      this._absX = this._x;
      this._absY = this._y;
    }
  }
  
  
  void addChild (ElementWindow child) 
  {
    this.children.add(child);
    child.parent = this;
    child.onNewParent();
  } 
  
  
  void onNewParent()
  {
    onPositionChange();
  }
  
  void removeChild(ElementWindow child) {
    this.children.removeRange(this.children.indexOf(child), 1);
  }
  
  void draw(){
    print("Draw method must be overridden");
  }
  
  int getAbsX()
  { 
    return _absX;
  }
  
  int getAbsY() {
    
    return _absY;
  }  
  
  ElementWindow getParent() {
    return this.parent;
  }
  
  void update(int time) {
    
    for(ElementWindow child in children)
    {
      child.update(time);
    }
  }
  
  CanvasWindow getParentCanvas()
  {
    ElementWindow current = this.parent;
    while(current != null && !(current is CanvasWindow || current is GameWindow))
    {
      current = current.parent;
    }
    
    return current;
  }
}

